package cn.buglife.springboot.controller.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by zhangjun on 15/11/30.
 */
@RequestMapping(value = "/redis")
@RestController
public class RedisController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 往redis里放入缓存内容
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Map put(){
        ValueOperations ops = stringRedisTemplate.opsForValue();
        String key = "cache_key";
        String value = "cache_value";
        Long timeout = 3600L;
        ops.set(key,value,timeout, TimeUnit.SECONDS);

        Map res = new HashMap();
        res.put("success",true);
        return res;
    }

    /**
     * 获取redis缓存里的内容
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Map get(){
        ValueOperations ops = stringRedisTemplate.opsForValue();
        String key = "cache_key";
        Map res = new HashMap();
        res.put(key,ops.get(key));
        return res;
    }
}
